const log = require('../util/log'),
    config = require('../util/config'),
    formatDate = require('../../lib/formatDate')

module.exports = (req, res) => {
    require('../util/service').handlePost(req, res, async (reqObj, resolve) => {
        const mongoMng = require('../mng/mongoMng')
        if (!mongoMng.db) {
            log('未连接mongo')
            resolve({
                result: -1,
                msg: '系统错误'
            })
            return;
        }
        if (!reqObj.page) {
            resolve({
                result: -1,
                msg: '缺少page'
            })
            return;
        }
        if (!reqObj.customer_phone) {
            resolve({
                result: -1,
                msg: '缺少customer_phone'
            })
            return;
        }
        var query = {}
        if (reqObj.customer_phone != '*') {
            query.customer_phone = reqObj.customer_phone == 'null' ? null : reqObj.customer_phone
        }
        mongoMng.db.collection('log').find(query).sort({
            time: -1,
            _id: -1
        }).skip(reqObj.page * 10 - 10).limit(10).toArray((err, result) => {
            if (err) {
                log(err);
                reject('系统错误')
                return;
            }
            var logs = []
            result.forEach(item => {
                logs.push({
                    link: item.link,
                    time: item.time,
                    customer_phone: item.customer_phone
                })
            })
            resolve({
                result: 1,
                logs: logs
            })
        })
    })
}
